﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media;

namespace Filas
{
    public class Rectangles
    {
        /// <summary>
        ///numRectangles -> maximum number of elements in the queue
        ///rectangles -> contains the queue elements
        /// </summary>
        private int numRectangles = 10;
        private ArrayList rectangles = new ArrayList();        

        public int numElements
        {
            get { return this.numRectangles; }
        }

        /// <summary>
        /// Creates the rectangles that represent each element of the queue. 
        /// All the elements are created at once. 
        /// Their visibility is what represents the insert and remove operations.
        /// </summary>
        /// <param name="gd"></param>
        /// <param name="myImages"></param>
        public void createRectangles(Grid gd, Images myImages)
        {
            for (int i = 0; i < numElements; i++)
            {
                Rectangle tempRectangle = new Rectangle();
                gd.Children.Add(tempRectangle);
                rectangles.Add(tempRectangle);
            }

            foreach (Rectangle r in rectangles)
            {
                r.Visibility = Visibility.Hidden;
                r.Stroke = Brushes.Black;
                r.VerticalAlignment = VerticalAlignment.Center;
                r.HorizontalAlignment = HorizontalAlignment.Stretch;
                r.Fill = myImages.getFillImage(rectangles.IndexOf(r));

                Grid.SetRow(r, 2);
                Grid.SetRowSpan(r, 2);
                Grid.SetColumnSpan(r, 6);
            }
        }

        /// <summary>
        /// Sets all rectangles visibility to hidden
        /// </summary>
        public void setAllHidden()
        {
            for (int i = 0; i < rectangles.Count; i++)
                ((Rectangle)rectangles[i]).Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// Sets the specific visibility to the rectangle at index "ind".
        /// </summary>
        /// <param name="ind"></param>
        /// <param name="visibility"></param>
        public void setVisibility(int ind, Visibility visibility)
        {
            ((Rectangle)rectangles[ind]).Visibility = visibility;
        }

        /// <summary>
        /// Returns the rectangle at index "ind"
        /// </summary>
        /// <param name="ind"></param>
        /// <returns></returns>
        public Rectangle getRectangle(int ind)
        {
            return ((Rectangle)this.rectangles[ind]);
        }

        /// <summary>
        /// To "simulate" the movement of people in the queue when one element is removed,
        /// the rectangles are filled with the next rectangle in the queue image. 
        /// The last one recieves the first one image. So, it's cyclic.
        /// </summary>
        /// <param name="numRetangulos"></param>
        internal void updateRectanglesFill(int numRetangulos)
        {
            ImageBrush temp = new ImageBrush();
            temp = (ImageBrush)((Rectangle)(rectangles[0])).Fill;

            for (int i = 0; i < numRetangulos - 1; i++)
            {
                ((Rectangle)(rectangles[i])).Fill = ((Rectangle)(rectangles[i + 1])).Fill;
            }

            ((Rectangle)(rectangles[numRetangulos - 1])).Fill = temp;
        }

        /// <summary>
        /// Sets the rectangles' margin and height when the grid size changes
        /// </summary>
        internal void gridSizeChanged()
        {
            for (int i = 0; i < numElements; i++)
            {
                ((Rectangle)(rectangles[i])).Margin = new Thickness((66 * (i + 1)), 6, (66 * (10 - i)), 80);
                ((Rectangle)(rectangles[i])).Height = 106;                
            }
        }
    }
}
